﻿Imports Oracle.DataAccess.Client
Public Class fListaCajas
    Private lbCaja As String
    Private lIdsede As String
    Dim daSedes As lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
    Public ReadOnly Property caja() As String
        Get
            Return lbCaja
        End Get
    End Property
    Public WriteOnly Property idSede() As String
        Set(ByVal value As String)
            lIdsede = value
        End Set
    End Property

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        Dim qSelect As String = "SELECT * FROM CAJ_CAJAS "
        Dim qWhere As String = " WHERE "
        Dim entro As Integer = 0
        If Me.txtCaja.Text.Trim.Length > 0 Then
            If entro > 0 Then
                qWhere &= " AND "
            End If
            entro += 1
            qWhere &= "DESCRIPCION LIKE '%" & Me.txtCaja.Text & "%'"
        End If
        If Me.cbSedes.SelectedValue <> "-1" Then
            If entro > 0 Then
                qWhere &= " AND "
            End If
            entro += 1
            qWhere &= "IDSUCURSAL='" & cbSedes.SelectedValue & "'"
        End If
        If entro > 0 Then
            qSelect &= qWhere
        End If
        cmd.CommandText = qSelect
        Try
            Conn.Open()
            DsCaja.CAJ_CAJAS.Rows.Clear()
            DsCaja.CAJ_CAJAS.Load(cmd.ExecuteReader)
            Conn.Close()
            FAC_CAJASDataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub CargaSedes()
        Try
            daSedes.Fill(DsGlobal.GLO_SEDES)
            Dim Dr As lbGlobalcom.dsGlobal.GLO_SEDESRow = DsGlobal.GLO_SEDES.NewGLO_SEDESRow
            Dr.IDSEDE = "-1"
            Dr.NOMBRE = "Todas"
            DsGlobal.GLO_SEDES.Rows.Add(Dr)
            Dr = Nothing
            If lIdsede.Trim.Length > 0 Then
                cbSedes.SelectedValue = lIdsede
                cbSedes.Enabled = False
            Else
                cbSedes.SelectedValue = "-1"
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub FAC_CAJASDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles FAC_CAJASDataGridView.CellMouseDoubleClick
        If e.RowIndex >= 0 Then
            lbCaja = FAC_CAJASDataGridView.Rows(e.RowIndex).Cells("IDCAJA").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub AsignaCaja(ByVal Fila As Integer)
        If Fila >= 0 Then
            lbCaja = FAC_CAJASDataGridView.Rows(Fila).Cells("IDCAJA").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub


    Private Sub FAC_CAJASDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FAC_CAJASDataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaCaja(FAC_CAJASDataGridView.CurrentRow.Index)
        End If
    End Sub

    Private Sub fListaCajas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daSedes = New lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
            CargaSedes()
        Catch ex As Exception

        End Try
    End Sub
End Class